{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Hello world"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello world!\n"
     ]
    }
   ],
   "source": [
    "print(\"Hello world!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-14.0\n",
      "-28.0\n"
     ]
    }
   ],
   "source": [
    "a=2*3-4*6*7/8+1\n",
    "print(a)\n",
    "print(2*a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7\n"
     ]
    }
   ],
   "source": [
    "# 整数\n",
    "myint = 7\n",
    "print(myint)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7.0\n",
      "7.0\n"
     ]
    }
   ],
   "source": [
    "# 浮点数\n",
    "myfloat = 7.0\n",
    "print(myfloat)\n",
    "myfloat = float(7)\n",
    "print(myfloat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello\n",
      "hello\n"
     ]
    }
   ],
   "source": [
    "# 字符串\n",
    "mystring = 'hello'\n",
    "print(mystring)\n",
    "mystring = \"hello\"\n",
    "print(mystring)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't worry about apostrophes\n"
     ]
    }
   ],
   "source": [
    "# 在双引号之间可以使用单引号，反之亦然\n",
    "mystring = \"Don't worry about apostrophes\"\n",
    "print(mystring)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "1\n",
      "2\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "# 列表\n",
    "mylist = []\n",
    "mylist.append(1)\n",
    "mylist.append(2)\n",
    "mylist.append(3)\n",
    "print(mylist[0]) # prints 1\n",
    "print(mylist[1]) # prints 2\n",
    "print(mylist[2]) # prints 3\n",
    "\n",
    "# prints out 1,2,3\n",
    "for x in mylist:\n",
    "    print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 5, 'a', 9, 'c']\n",
      "[1, 2, 99, 'a', 9, 'c']\n",
      "[1, 2, 99, 'a', 'c']\n",
      "[[10, 20]]\n"
     ]
    }
   ],
   "source": [
    "list1=[1,2,5,'a',9,'c'] # or list(1,2,5,'a',9,'c')\n",
    "print(list1)\n",
    "len(list1) # 计数\n",
    "list1[0:2] # 索引\n",
    "list1[2:-1]\n",
    "list1[2]=99 # 改变索引为2处的值\n",
    "print(list1)\n",
    "#list1.append(100) # 等价于 list1=list1+[100]\n",
    "del list1[4] # 删除\n",
    "print(list1)\n",
    "list3=[] # 建立空的列表，以便后续添加元素\n",
    "list3.append([10,20]) # 向原来列表追加新的列表或元素\n",
    "print(list3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 3, 'a')\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "'tuple' object does not support item assignment",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-9-e8dd69ad51f9>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[0mtuple1\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'a'\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# 等价于 tuple1=tuple(1,3,'a')\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtuple1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[0mtuple1\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m10\u001b[0m  \u001b[1;31m# 是错误的命令，因为元组不可改变里面元素。\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "# 元组\n",
    "tuple1=(1,3,'a') # 等价于 tuple1=tuple(1,3,'a')\n",
    "print(tuple1)\n",
    "tuple1[1]=10  # 是错误的命令，因为元组不可改变里面元素。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': ['xiao ming', 'xiao hong'], 'age': [20, 22], 'score': [85, 90], 'address': 'Qin Yuan,Ze Yuan'}\n"
     ]
    }
   ],
   "source": [
    "# 字典\n",
    "dict1={\"name\":[\"xiao ming\",\"xiao hong\"],\"age\":[20,22],\"score\":[85,90]} # 字典成对定义的，冒号前面称为'key',后面为“value”.\n",
    "dict1[\"name\"]  # 索引\n",
    "dict1[\"score\"] # 索引\n",
    "dict1[\"address\"]=\"Qin Yuan,Ze Yuan\" # 添加\n",
    "print(dict1)\n",
    "del dict1[\"score\"] # 删除\n",
    "dict1.items() # 以列表返回可遍历的(键, 值) 元组数组\n",
    "dict2={} # 建立空的字典，以便后续添加元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n",
      "----------\n",
      "False\n",
      "True\n",
      "True\n",
      "----------\n",
      "True\n",
      "False\n",
      "----------\n",
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "# 布尔型\n",
    "print(True)\n",
    "print(False)\n",
    "print('-' * 10)\n",
    "print(True and False)\n",
    "print(False or True)\n",
    "print(not False)\n",
    "print('-' * 10)\n",
    "print(bool(2))\n",
    "print(bool([]))\n",
    "print('-' * 10)\n",
    "print(1<2)\n",
    "print(2>4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 条件语句 , if--else 句式, if--elif--else"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This sentence may be related to Nanjing\n"
     ]
    }
   ],
   "source": [
    "str1=\"I start my graduate study at Nanjing Audit University \"\n",
    "if 'Nanjing' in str1:\n",
    "    print(\"This sentence may be related to Nanjing\")\n",
    "else:\n",
    "    print(\"I cannot make any correct judge\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This sentence may be related to NAU\n"
     ]
    }
   ],
   "source": [
    "if 'nanjing' in str1:\n",
    "    print(\"This sentence may be related to Nanjing\")\n",
    "elif 'nanjing' not in str1 and 'Audit' in str1:\n",
    "    print(\"This sentence may be related to NAU\")\n",
    "else:\n",
    "    print(\"I cannot make any correct judge\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 循环语句，for 句式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
      "0 1 2 3 4 5 6 7 8 9 \n",
      "0 2 4 6 8 \n",
      "0 1 2 3 4 5 6 7 8 9 \n",
      "a = 1\n",
      "b = 2\n",
      "c = 3\n"
     ]
    }
   ],
   "source": [
    "num = range(10)\n",
    "print(list(num))\n",
    "\n",
    "for i in range(10):\n",
    "    print(i, end=\" \")\n",
    "print()\n",
    "\n",
    "for i in range(10):\n",
    "    if i % 2==0:\n",
    "        print(i, end=\" \")\n",
    "print()\n",
    "\n",
    "for i in tuple(range(10)):\n",
    "    print(i, end=\" \")\n",
    "print()\n",
    "\n",
    "d = {'a': 1, 'b': 2, 'c': 3} # dict\n",
    "for k in d:\n",
    "    print(f'{k} = {d[k]}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<function <lambda> at 0x000001FE231D7730>\n",
      "3\n",
      "<function f2 at 0x000001FE231D7400>\n",
      "3\n",
      "<function f3 at 0x000001FE231D7620>\n",
      "[0, 4, 1, 10, 2, 16, 3, 22, 4, 28]\n"
     ]
    }
   ],
   "source": [
    "# 函数\n",
    "f1 = lambda x: x + 1\n",
    "print(f1)\n",
    "print(f1(2))\n",
    "\n",
    "def f2(x):\n",
    "    return x + 1\n",
    "print(f2)\n",
    "print(f2(2))\n",
    "\n",
    "def f3(x):\n",
    "    if x % 2 == 0:\n",
    "        return x // 2\n",
    "    else:\n",
    "        return 3*x + 1\n",
    "print(f3)\n",
    "print([f3(i) for i in range(10)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "blah\n",
      "64\n"
     ]
    }
   ],
   "source": [
    "# 类\n",
    "class MyClass:\n",
    "    variable = \"blah\"\n",
    "\n",
    "    def function(self):\n",
    "        print(\"This is a message inside the class.\")\n",
    "    \n",
    "    def f3(self, x):\n",
    "        if x % 2 == 0:\n",
    "            return x // 2\n",
    "        else:\n",
    "            return 3*x + 1\n",
    "\n",
    "myobjectx = MyClass()\n",
    "print(myobjectx.variable)\n",
    "print(myobjectx.f3(21))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "输入一个整数> 34\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x square is 1156\n"
     ]
    }
   ],
   "source": [
    "# 输入输出\n",
    "x = input('输入一个整数>')\n",
    "x = int(x)\n",
    "print(f'x square is {x * x}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
